﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMathLibrary.Geometry
{
    /// <summary>
    /// This class descries a plane rectangle. 
    /// </summary>
    [Serializable]
    public class Rectangle : GeometricObject, IRectangle
    {
        /// <summary>
        /// The first point of the rectangle (left-bottom).
        /// </summary>
        private Point2D pointA;

        /// <summary>
        /// The second point of the rectangle (right-top).
        /// </summary>
        private Point2D pointB;

        /// <summary>
        /// Initializes a new instance of the <see cref="Rectangle"/> class.
        /// </summary>
        public Rectangle()
        {
            this.pointA = new Point2D();
            this.pointB = new Point2D();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Rectangle"/> class.
        /// </summary>
        /// <param name="pointA">The first point of the rectangle (left-bottom).</param>
        /// <param name="pointB">The second point of the rectangle (right-top).</param>
        public Rectangle(Point2D pointA, Point2D pointB)
        {
            this.pointA = pointA;
            this.pointB = pointB;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Rectangle"/> class.
        /// </summary>
        /// <param name="rectangle">The rectangle to clone.</param>
        public Rectangle(Rectangle rectangle)
        {
            if (rectangle == (Rectangle) null)
            {
                throw new ArgumentNullException("rectangle");
            }

            this.pointA = rectangle.PointA;
            this.pointB = rectangle.PointB;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Rectangle"/> class.
        /// </summary>
        /// <param name="pointA">The first point of the rectangle (left-bottom).</param>
        /// <param name="width">The width of the rectangle.</param>
        /// <param name="height">The height of the rectangle.</param>
        public Rectangle(Point2D pointA, double width, double height)
        {
            if (pointA == (Point2D) null)
            {
                throw new ArgumentNullException("pointA");
            }

            this.pointA = pointA;
            this.pointB = pointA;
            this.pointA.X += width;
            this.pointA.Y += height;
        }

        /// <summary>
        /// Gets or sets the first point of the rectangle (left-bottom).
        /// </summary>
        /// <value>The first point of the rectangle (left-bottom).</value>
        public Point2D PointA
        {
            get { return pointA; }
            set { pointA = value; }
        }

        /// <summary>
        /// Gets or sets the second point of the rectangle (right-top).
        /// </summary>
        /// <value>The second point of the rectangle (right-top).</value>
        public Point2D PointB
        {
            get { return pointB; }
            set { pointB = value; }
        }

        /// <summary>
        /// Compares the current instance to another rectangle.
        /// </summary>
        /// <param name="compare">The rectangle to compare.</param>
        /// <returns>True if the rectangle positions are even otherwise, false.</returns>
        public bool CompareTo(Rectangle compare)
        {
            return this == compare;
        }

        /// <summary>
        /// Gets the height of the rectangle.
        /// </summary>
        /// <value>The height of the rectangle.</value>
        public double Height
        {
            get { return ExtendedMath.Fabs(pointB.Y - pointA.Y); }
        }

        /// <summary>
        /// Gets the width of the rectangle.
        /// </summary>
        /// <value>The width of the rectangle.</value>
        public double Width
        {
            get { return ExtendedMath.Fabs(pointB.X - pointA.X); }
        }

        /// <summary>
        /// Implements the operator ==.
        /// </summary>
        /// <param name="a">The first rectangle.</param>
        /// <param name="b">The second rectangle.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator ==(Rectangle a, Rectangle b)
        {
            if (System.Object.ReferenceEquals(a, b))
            {
                return true;
            }

            if (((object) a == null) || ((object) b == null))
            {
                return false;
            }

            return ((a.PointA == b.PointA) && (a.PointB == b.PointB));
        }

        /// <summary>
        /// Implements the operator !=.
        /// </summary>
        /// <param name="a">The first rectangle.</param>
        /// <param name="b">The second rectangle.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator !=(Rectangle a, Rectangle b)
        {
            return !(a == b);
        }

        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <exception cref="T:System.NullReferenceException">The <paramref name="obj"/> parameter is null.</exception>
        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        /// <summary>
        /// Serves as a hash function for a particular type.
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        public override string ToString()
        {
            return this.pointA.ToString() + " | " + this.pointB.ToString();
        }
    }
}